
import { getAction } from '@/api/manage'
import Vue from 'vue'
import { mapGetters } from 'vuex';
import { ACCESS_TOKEN } from '@/store/mutation-types'
export const TaskRedistribution = {
  data() {
    return {
      tokenHeader: { 'Authorization': Vue.ls.get(ACCESS_TOKEN), 'client-id': 'web', 'user-id': Vue.ls.get('user_id') || '' },
      receivablePaymentLoading: false,
      receivablePaymentColumns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '款项名称',
          align: 'center',
          dataIndex: 'paymentName',
          width: 260,
          ellipsis: true
        },
        {
          title: '款项节点说明',
          align: 'center',
          dataIndex: 'paymentDesc',
          width: 260,
          ellipsis: true
        },
        {
          title: '款项关联任务',
          align: 'center',
          dataIndex: 'taskId_dictText',
          width: 260,
          ellipsis: true
        },
        {
          title: '需分配款项金额(元)',
          align: 'center',
          dataIndex: 'isAllowDistributionMoney',
          width: 140,
          ellipsis: true
        },
        {
          title: '应收状态',
          align: 'center',
          width: 140,
          dataIndex: 'shouldPayFlag',
          ellipsis: true,
          customRender: (text) => {
            if (text && text == 1) {
              return '已生成'
            } else {
              return '未生成'
            }
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 150,
          scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action'
          }
        }
      ],
      receivablePaymentList: [] // 需分配的款项信息
    }
  },
  methods: {
    ...mapGetters(['userInfo']), // 用户信息
    // 获取需分配的款项信息
    getReceivablePaymentList(taskId, mainId, type) {
      if (!taskId) {
        this.receivablePaymentList = []
        return
      }
      var params = {
        taskId: taskId
      }
      if (mainId) {
        params.mainId = mainId
      }
      if (type) {
        params.type = type
      }
      this.receivablePaymentLoading = true
      getAction('/api/erp/task/erpOmcTaskRedistribution/queryReceivablePayment', params).then(res => {
        if (res.success) {
          if (res.result && res.result.length > 0) {
            res.result.map((value, index) => {
              res.result[index].taskId_dictText = '' // 首先款项关联任务字段默认为空
              // 遍历planTaskList数组赋值给taskId_dictText
              res.result[index].taskId_dictText = value.planTaskVoList && value.planTaskVoList.length ? (value.planTaskVoList.map((obj) => { return obj.categoryName }).join(',')) : '';
            })
            this.receivablePaymentList = res.result
          } else {
            this.receivablePaymentList = []
          }
        } else {
          this.receivablePaymentList = []
        }
      }).catch(() => {
        this.receivablePaymentList = []
      }).finally(() => {
        this.receivablePaymentLoading = false
      })
    },
    submitPaymentModal (data) {
      this.receivablePaymentLoading = true;
      let flag = true;
      // debugger;
      for (let i = 0; i < this.receivablePaymentList.length; i++) {
        if (this.receivablePaymentList[i].id == data.id) {
          this.receivablePaymentList.splice(i, 1, data);
          flag = false;
        }
      }
      if (flag) {
        this.receivablePaymentList.push(data);
      }
      this.receivablePaymentLoading = false;
    }
  }

}
